﻿#include "RestlessJar.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char RestlessJar::getAttackRange() const
    {
        return 6;
    }

    bool RestlessJar::getCanMove() const
    {
        return false;
    }

    bool RestlessJar::getCanRegen() const
    {
        return false;
    }

    bool RestlessJar::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    RestlessJar::RestlessJar(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void RestlessJar::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        if (!ranged)
        {
            auto hpPercent = (HP * 100) / Stats[Stat::HP];

            switch (getEnvir()->Random->Next(3))
            {
                case 0: //Spin
                case 1:
                {
                        int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);

                        S::ObjectAttack *tempVar = new S::ObjectAttack();
                        tempVar->ObjectID = ObjectID;
                        tempVar->Direction = getDirection();
                        tempVar->Location = getCurrentLocation();
                        tempVar->Type = 0;
                        Broadcast(tempVar);
                        if (damage == 0)
                        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                            return;
                        }

                        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MACAgility, static_cast<int>(0)});
                        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                }
                    break;
                case 2:
                {
                        if (hpPercent >= 50) //Tornado
                        {
                            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

                            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
                            tempVar2->ObjectID = ObjectID;
                            tempVar2->Direction = getDirection();
                            tempVar2->Location = getCurrentLocation();
                            tempVar2->TargetID = getTarget()->ObjectID;
                            tempVar2->Type = 1;
                            Broadcast(tempVar2);
                            if (damage == 0)
                            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                                return;
                            }

                            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MACAgility, static_cast<int>(1)});
                            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                        }
                        else //Stomp
                        {
                            int damage = Stats[Stat::MaxDC];

                            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
                            tempVar3->ObjectID = ObjectID;
                            tempVar3->Direction = getDirection();
                            tempVar3->Location = getCurrentLocation();
                            tempVar3->Type = 2;
                            Broadcast(tempVar3);
                            if (damage == 0)
                            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                                return;
                            }

                            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MACAgility, static_cast<int>(2)});
                            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                        }
                }
                    break;
            }

            ActionTime = getEnvir()->getTime() + 300;
            AttackTime = getEnvir()->getTime() + AttackSpeed;
        }
        else
        {
            S::ObjectRangeAttack *tempVar4 = new S::ObjectRangeAttack();
            tempVar4->ObjectID = ObjectID;
            tempVar4->Direction = getDirection();
            tempVar4->Location = getCurrentLocation();
            tempVar4->TargetID = getTarget()->ObjectID;
            tempVar4->Type = 0;
            Broadcast(tempVar4);
            ProjectileAttack(Stats[Stat::MinMC], Stats[Stat::MaxMC]);

            ActionTime = getEnvir()->getTime() + 300;
            AttackTime = getEnvir()->getTime() + (AttackSpeed * 2);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void RestlessJar::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        int type = std::any_cast<int>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        switch (type)
        {
            case 0:
            {
                    auto targets = FindAllTargets(1, getCurrentLocation());

                    for (int i = 0; i < targets.size(); i++)
                    {
                        if (targets[i]->Attacked(this, damage, defence) <= 0)
                        {
                            continue;
                        }
                    }
            }
                break;
            case 1:
            {
                    if (target->Attacked(this, damage, defence) <= 0)
                    {
                        return;
                    }
                    PoisonTarget(target, 4, 10, PoisonType::Blindness, 1000);
            }
                break;
            case 2:
            {
                    auto targets = FindAllTargets(1, getCurrentLocation());

                    for (int i = 0; i < targets.size(); i++)
                    {
                        if (targets[i]->Attacked(this, damage, defence) <= 0)
                        {
                            return;
                        }

                        MirDirection dir = Functions::DirectionFromPoint(getCurrentLocation(), targets[i]->getCurrentLocation());
                        targets[i]->Pushed(this, dir, 1);
                    }
            }
                break;
        }
    }
}
